class Solution(object):
    def minInsertions(self, s):
        l = 0
        r = 0
        ans = 0
        for c in s:
            if c == ')':
                r += 1
                if l == 0:
                    ans += 1
                    l += 1
                else:
                    if r == 2:
                        l -= 1
                        r = 0
            else:
                if r == 1:
                    ans += 1
                    r = 0
                else:
                    l += 1
        return ans + 2 * l - r

data = Solution()
s = "(()))(()))()())))"
print(data.minInsertions(s))